﻿#include "maintainpassword.h"
#include "ui_maintainpassword.h"
#include "mainwindow.h"

extern intListener passwordfl;

maintainPassword::maintainPassword(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::maintainPassword)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
    setAttribute(Qt::WA_QuitOnClose,false);
    ui->lineEdit->setInputMethodHints(Qt::ImhDigitsOnly);
}

maintainPassword::~maintainPassword()
{
    delete ui;
}

void maintainPassword::showEvent(QShowEvent *event){
    if(MainWindow::passWordType==0){
        ui->lineEdit->setValidator(new QRegExpValidator(QRegExp("[0-9]{6}")));
        ui->lineEdit->setEchoMode(QLineEdit::Password);
    }else{
        ui->lineEdit->setValidator(new QRegExpValidator(QRegExp("[A-Za-z0-9]{8}")));
        ui->lineEdit->setEchoMode(QLineEdit::Password);
    }
    ui->lineEdit->clear();
    ui->lineEdit->setFocus();
}

void maintainPassword::on_pushButton_4_clicked()
{
    this->close();
}

void maintainPassword::on_pushButton_5_clicked()
{
    if(MainWindow::passWordType==0){
        eqptinfo *eqInfo=new eqptinfo();
        QString password = eqInfo->passwordRead();
        QString inputPassWord=ui->lineEdit->text();
        if(inputPassWord==password){
            passwordfl.set(1);
            emit changeMacoWindow();
            this->close();
        }
        else
        {
            tcts1 = new tcts();
            tcts1->infochange("提示","error","您输入的密码错误，请重新输入",false,true);
            // 获取屏幕的中心点
            QRect screenGeometry = QApplication::desktop()->screenGeometry();
            int centerX = screenGeometry.center().x();
            int centerY = screenGeometry.center().y();
            int labelWidth = tcts1->sizeHint().width(); // 获取QLabel的建议大小
            int labelHeight = tcts1->sizeHint().height();
            int labelX = centerX-labelWidth/2;
            int labelY = centerY-labelHeight/2;
            tcts1->show();
            tcts1->move(labelX, labelY);
            ui->lineEdit->clear();
            ui->lineEdit->setFocus();
        }
    }else{
        QString inputPassWord=ui->lineEdit->text();

        QSqlDatabase db = dbHelper::openConnection();
        QSqlQuery query(db);

        QString savePassWord;
        QString sql="Select passWord From  IsFirstStartUp";
        query.exec(sql);
        while (query.next()) {
            savePassWord=query.value("PassWord").toString();
        }

        if(inputPassWord==savePassWord){
            passwordfl.set(1);
            emit changeAnalyConfigWindow();
            this->close();
        }
        else
        {
            tcts1 = new tcts();
            tcts1->infochange("提示","error","您输入的密码错误，请重新输入",false,true);
            // 获取屏幕的中心点
            QRect screenGeometry = QApplication::desktop()->screenGeometry();
            int centerX = screenGeometry.center().x();
            int centerY = screenGeometry.center().y();
            int labelWidth = tcts1->sizeHint().width(); // 获取QLabel的建议大小
            int labelHeight = tcts1->sizeHint().height();
            int labelX = centerX-labelWidth/2;
            int labelY = centerY-labelHeight/2;
            tcts1->show();
            tcts1->move(labelX, labelY);
            ui->lineEdit->clear();
            ui->lineEdit->setFocus();
        }
    }

}



void maintainPassword::on_lineEdit_textChanged(const QString &arg1)
{

}
